package com.yy.user.jpa;

import com.yy.user.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

/**
 * @author yangyao
 * @Description: User JPA
 * @date 2018/8/23 14:47
 */
@Repository
public interface UserRepository extends JpaRepository<User,Long> {
    /**
     * 根据用户名查找用户信息
     * @param username
     * @return User
     */
    public User findByUsername(String username);

    /**
     * 查询用户
     */
    public List<User> findAll();

    /**
     * 根据id查询用户
     */
    public Optional<User> findById(Long id);

    /**
     * 保存用户
     */
    @SuppressWarnings("unchecked")
    public User save(User user);

    /**
     * 删除用户
     */
    @Modifying
    @Query(value = "delete from t_user where id=?1", nativeQuery = true)
    public void delete(@Param("id")Long id);
}
